package cn.innosmart.aq.customize;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.LanSearchResultAdapter;
import cn.innosmart.aq.bean.ActionBean;
import cn.innosmart.aq.bean.CommandBean;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.bean.TriggerBean;
import cn.innosmart.aq.bean.ValueConditionBean;
import cn.innosmart.aq.util.DeviceConstant;
import cn.innosmart.aq.util.SystemConstant;
import cn.innosmart.aq.view.activity.RuleActivity;
import cn.innosmart.aq.view.activity.SceneActivity;

public class CustomsizeDialog extends Dialog {

    private Context mContext;

    public CustomsizeDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    public CustomsizeDialog(Context context) {
        super(context, R.style.dialog);
        init(context);
    }


    private static final int CHANGE_LOADINGDOT_WHAT = 1;
    private static final int CHANGE_TITLE_WHAT = 2;
    private static final int CHNAGE_TITLE_DELAYMILLIS = 1000;
    private static final int MAX_SUFFIX_NUMBER = 3;
    private static final char SUFFIX = '.';

    private ImageView iv_route;
    private TextView tv_point;
    private TextView tv_timer;
    private RotateAnimation mAnim;
    private RelativeLayout rl_title;
    private TextView tv_title;
    private TextView tv_loading;
    private RelativeLayout rl_result_listview;
    private ListView lv_result;
    private boolean cancelable = true;
    private RelativeLayout rl_loading_text;
    private RelativeLayout rl_string_content;
    private RelativeLayout rl_auto_content;
    private TextView tvConditionTitle;
    private TextView tvCondition;
    private TextView tvCommandTitle;
    private TextView tvCommand;
    private TextView tvValidTitle;
    private TextView tvValid;
    private TextView tvStatusTitle;
    private TextView tvStatus;
    private TextView tvPushTitle;
    private TextView tvPush;
    private TextView tvImportanceTilte;
    private TextView tvImportance;
    private LinearLayout llCondition;
    private LinearLayout llCommnad;
    private LinearLayout llValid;
    private LinearLayout llStatus;
    private LinearLayout llPush;
    private LinearLayout llImportance;
    private RelativeLayout rl_progress_content;
    private RelativeLayout rl_rule_trigger_condition;
    private LinearLayout ll_value_choose;
    private RadioGroup ll_value_input;
    private RadioButton rb_above;
    private RadioButton rb_below;
    private RadioGroup rg_choose;
    private RelativeLayout rl_background;
    private LinearLayout ll_value_check;
    private RadioButton rb_open;
    private RadioButton rb_close;
    private RelativeLayout rl_rule_action_condition;
    private LinearLayout ll_sleep;
    private EditText et_sleep;

    private Handler handler = new Handler() {
        private int num = 0;

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CHANGE_LOADINGDOT_WHAT:
                    loadingPoint();
                    if (isShowing()) {
                        handler.sendEmptyMessageDelayed(CHANGE_LOADINGDOT_WHAT, CHNAGE_TITLE_DELAYMILLIS);
                        showTimerCount();
                    } else {
                        num = 0;
                    }
                    break;
                case CHANGE_TITLE_WHAT:
                    String string = (String) msg.obj;
                    tv_title.setText(string);
                    break;
            }
        }

        private void showTimerCount() {
            if (isTimer) {
                tv_timer.setText(String.valueOf(TimeCount));
                TimeCount--;
            }
        }

        private void loadingPoint() {
            if (isPointShow) {
                StringBuilder builder = new StringBuilder();
                if (num >= MAX_SUFFIX_NUMBER) {
                    num = 0;
                }
                num++;
                for (int i = 0; i < num; i++) {
                    builder.append(SUFFIX);
                }
                tv_point.setText(builder.toString());
            }
        }

        ;

    };


    private void init(Context context) {
        this.mContext = context;
        View contentView = View.inflate(getContext(), R.layout.customsize_dialog, null);
        AutoUtils.autoSize(contentView);
        setContentView(contentView);
        tv_loading = (TextView) findViewById(R.id.tv_loading);
        iv_route = (ImageView) findViewById(R.id.iv_route);
        tv_point = (TextView) findViewById(R.id.tv_point);
        tv_timer = (TextView) findViewById(R.id.tv_timer);
        rl_title = (RelativeLayout) findViewById(R.id.rl_title);
        tv_title = (TextView) findViewById(R.id.tv_title);
        bt_positive = (Button) findViewById(R.id.bt_positive);
        bt_negative = (Button) findViewById(R.id.bt_negative);
        bt_negative.setOnClickListener(mBtnOnClickListener);
        bt_positive.setOnClickListener(mBtnOnClickListener);
        tv_content = (TextView) findViewById(R.id.tv_content);
        rl_auto_content = (RelativeLayout) findViewById(R.id.rl_auto_content);
        tvConditionTitle = (TextView) findViewById(R.id.tv_condition_title);
        tvCondition = (TextView) findViewById(R.id.tv_condition);
        tvCommandTitle = (TextView) findViewById(R.id.tv_command_title);
        tvCommand = (TextView) findViewById(R.id.tv_command);
        tvValidTitle = (TextView) findViewById(R.id.tv_valid_title);
        tvValid = (TextView) findViewById(R.id.tv_valid);
        tvStatusTitle = (TextView) findViewById(R.id.tv_status_title);
        tvStatus = (TextView) findViewById(R.id.tv_status);
        llCondition = (LinearLayout) findViewById(R.id.ll_condition);
        llCommnad = (LinearLayout) findViewById(R.id.ll_command);
        llValid = (LinearLayout) findViewById(R.id.ll_valid);
        llStatus = (LinearLayout) findViewById(R.id.ll_status);
        tvPushTitle = (TextView) findViewById(R.id.tv_push_title);
        tvPush = (TextView) findViewById(R.id.tv_push);
        tvImportanceTilte = (TextView) findViewById(R.id.tv_importance_title);
        tvImportance = (TextView) findViewById(R.id.tv_importance);
        llPush = (LinearLayout) findViewById(R.id.ll_push_enable);
        llImportance = (LinearLayout) findViewById(R.id.ll_importance);
        rl_result_listview = (RelativeLayout) findViewById(R.id.rl_result_listview);
        lv_result = (ListView) findViewById(R.id.lv_result);
        rl_loading_text = (RelativeLayout) findViewById(R.id.rl_loading_text);
        rl_string_content = (RelativeLayout) findViewById(R.id.rl_string_content);
        tv_result_info = (TextView) findViewById(R.id.tv_result_info);
        rl_progress_content = (RelativeLayout) findViewById(R.id.rl_progress_content);
        ll_value_choose = (LinearLayout) findViewById(R.id.ll_value_choose);
        ll_value_input = (RadioGroup) findViewById(R.id.ll_value_input);
        rg_choose = (RadioGroup) findViewById(R.id.rg_choose);
        rb_above = (RadioButton) findViewById(R.id.rb_above);
        rb_below = (RadioButton) findViewById(R.id.rb_below);
        ll_value_check = (LinearLayout) findViewById(R.id.ll_value_check);
        rl_background = (RelativeLayout) findViewById(R.id.rl_background);
        rb_open = (RadioButton) findViewById(R.id.rb_open);
        rb_close = (RadioButton) findViewById(R.id.rb_close);
        et_sleep = (EditText) findViewById(R.id.et_sleep);
        rl_rule_action_condition = (RelativeLayout) findViewById(R.id.rl_rule_action_condition);
        ll_sleep = (LinearLayout) findViewById(R.id.ll_sleep);
        initTriggerLayout();
        initAnim();
        isTimer = false;
        TimeCount = 30;
        timerOver = null;
        //getWindow().setWindowAnimations(R.anim.alpha_in);


    }

    private RadioButton cbAbove;
    private RadioButton cbBelow;
    private EditText etTriggerValue;
    private TextView tvSensorUnity;

    private void initTriggerLayout() {
        rl_rule_trigger_condition = (RelativeLayout) findViewById(R.id.rl_rule_trigger_condition);
        cbAbove = (RadioButton) findViewById(R.id.cb_above);
        cbBelow = (RadioButton) findViewById(R.id.cb_below);
        etTriggerValue = (EditText) findViewById(R.id.et_trigger_value);
        tvSensorUnity = (TextView) findViewById(R.id.tv_sensor_unity);
    }


    private boolean isPointShow = true;

    public void setLoadingText(CharSequence content) {
        if (content == null) {
            isPointShow = false;
            rl_loading_text.setVisibility(View.GONE);
        } else {
            rl_loading_text.setVisibility(View.VISIBLE);
        }
        rl_progress_content.setVisibility(View.VISIBLE);
        tv_loading.setText(content);
    }


    public void setLoadingText(int contentId) {
        rl_loading_text.setVisibility(View.VISIBLE);
        rl_progress_content.setVisibility(View.VISIBLE);
        tv_loading.setText(getContext().getString(contentId));
    }

    private void initAnim() {
        mAnim = new RotateAnimation(360, 0, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
        mAnim.setDuration(2000);
        mAnim.setRepeatCount(Animation.INFINITE);
        mAnim.setRepeatMode(Animation.RESTART);
        mAnim.setStartTime(Animation.START_ON_FIRST_FRAME);
    }

    @Override
    public void show() {
        TimeCount = 30;
        iv_route.startAnimation(mAnim);
        handler.sendEmptyMessage(CHANGE_LOADINGDOT_WHAT);
        if (adapter != null) {
            if (adapter.getCount() == 0) {
                lv_result.setVisibility(View.GONE);
                tv_result_info.setText(R.string.no_search_result);
            }
        }
        if (isTimer) {
            tv_timer.setVisibility(View.VISIBLE);
        } else {
            tv_timer.setVisibility(View.GONE);
        }
        super.show();
    }

    @Override
    public void dismiss() {
        mAnim.cancel();
        super.dismiss();
    }


    @Override
    public void setCancelable(boolean flag) {
        cancelable = flag;
        super.setCancelable(flag);
    }

    @Override
    public void setTitle(CharSequence title) {
        rl_title.setVisibility(View.VISIBLE);
        tv_title.setText(title);
    }

    public void changeTitle(CharSequence title) {
        rl_title.setVisibility(View.VISIBLE);
        Message msg = handler.obtainMessage();
        msg.what = CHANGE_TITLE_WHAT;
        msg.obj = title.toString();
        handler.sendMessage(msg);
    }

    public void changeTitle(int titleId) {
        rl_title.setVisibility(View.VISIBLE);
        Message msg = handler.obtainMessage();
        msg.what = CHANGE_TITLE_WHAT;
        msg.obj = getContext().getString(titleId);
        handler.sendMessage(msg);
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getContext().getString(titleId));
    }

    @Override
    public void onBackPressed() {
    }

    private TextView tv_result_info;
    private boolean isTimer = false;
    private int TimeCount = 30;
    public static TimerOver timerOver;

    public void setTimerOver(TimerOver callback) {
        isTimer = true;
        timerOver = callback;
    }

    private BaseAdapter adapter;

    public void setListviewAdapter(BaseAdapter baseAdapter) {
        rl_string_content.setVisibility(View.GONE);
        rl_loading_text.setVisibility(View.GONE);
        rl_result_listview.setVisibility(View.VISIBLE);
        this.adapter = baseAdapter;
        lv_result.setAdapter(baseAdapter);
    }

    public void setOnItemClickedListener(AdapterView.OnItemClickListener itemclick) {
        lv_result.setOnItemClickListener(itemclick);
    }

    public void changeTriggerValue(TriggerBean triggerBean) {
        String address = triggerBean.getAddress();
        DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(address);
        setTriggerCondition(deviceBean);
        int functype = deviceBean.getFunctype();
        ArrayList<ValueConditionBean> valueConditionBeans = triggerBean.getValueConditionBeans();
        ValueConditionBean valueConditionBean = valueConditionBeans.get(0);
        double value = valueConditionBean.getThreshold();
        if (functype == DeviceConstant.FUNCTYPE_WATERLEVER) {
            if (rb_above.isChecked()) {
                valueConditionBean.setThreshold(2);
            } else {
                valueConditionBean.setThreshold(1);
            }
        } else {
            if (cbAbove.isChecked()) {
                String aboveValue = etTriggerValue.getText().toString().trim();
                if (!TextUtils.isEmpty(aboveValue)) {
                    valueConditionBean.setOp(">");
                    valueConditionBean.setThreshold(DeviceConstant.convertValue(deviceBean.getSensorunit(), aboveValue));
                }
            } else {
                String belowValue = etTriggerValue.getText().toString().trim();
                if (!TextUtils.isEmpty(belowValue)) {
                    valueConditionBean.setOp("<");
                    valueConditionBean.setThreshold(DeviceConstant.convertValue(deviceBean.getSensorunit(), belowValue));
                }
            }
        }
    }

    public void showTriggerValue(TriggerBean triggerBean) {
        rl_rule_trigger_condition.setVisibility(View.VISIBLE);
        String address = triggerBean.getAddress();
        DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(address);
        int functype = deviceBean.getFunctype();
        ArrayList<ValueConditionBean> valueConditionBeans = triggerBean.getValueConditionBeans();
        ValueConditionBean valueConditionBean = valueConditionBeans.get(0);
        double value = valueConditionBean.getThreshold();
        if (functype == DeviceConstant.FUNCTYPE_WATERLEVER) {
            ll_value_choose.setVisibility(View.VISIBLE);
            ll_value_input.setVisibility(View.GONE);
            if (value == 1) {
                rb_below.setChecked(true);
            } else if (value == 2) {
                rb_above.setChecked(true);
            }
        } else if (functype == DeviceConstant.FUNCTYPE_SWITCH) {
            ll_value_choose.setVisibility(View.VISIBLE);
            ll_value_input.setVisibility(View.GONE);
            if (value == 1) {
                rb_below.setChecked(true);
            } else if (value == 2) {
                rb_above.setChecked(true);
            }
        } else if (functype == DeviceConstant.FUNCTYPE_PH) {
            ll_value_choose.setVisibility(View.GONE);
            ll_value_input.setVisibility(View.VISIBLE);
            if (valueConditionBean.getOp().equals(">")) {
                cbAbove.setChecked(true);
            } else if (valueConditionBean.getOp().equals("<")) {
                cbBelow.setChecked(true);
            }
            etTriggerValue.setText(String.valueOf(value));
            tvSensorUnity.setText("");
        } else if (functype == DeviceConstant.FUNCTYPE_TEMPERATURE) {
            ll_value_choose.setVisibility(View.GONE);
            ll_value_input.setVisibility(View.VISIBLE);
            if (valueConditionBean.getOp().equals(">")) {
                cbAbove.setChecked(true);
            } else if (valueConditionBean.getOp().equals("<")) {
                cbBelow.setChecked(true);
            }
            if (deviceBean.getSensorunit() == 1) {
                value = value / 10;
            }
            etTriggerValue.setText(String.valueOf(value));
            tvSensorUnity.setText(mContext.getString(R.string.temperature_unity));
        }
    }

    public static interface TimerOver {
        void onTimerOver();
    }

    private TextView tv_content;
    private Button bt_positive;
    private Button bt_negative;
    private CharSequence mPositiveButtonText;
    private View.OnClickListener mPositiveButtonListener;
    private View.OnClickListener mNegativeButtonListener;
    private CharSequence mNegativeButtonText;

    public void setPositiveButton(int textId, View.OnClickListener listener) {
        bt_positive.setVisibility(View.VISIBLE);
        mPositiveButtonText = mContext.getText(textId);
        bt_positive.setText(mPositiveButtonText);
        mPositiveButtonListener = listener;
    }

    public void setPositiveButton(CharSequence text, View.OnClickListener listener) {
        bt_positive.setVisibility(View.VISIBLE);
        mPositiveButtonText = text;
        bt_positive.setText(mPositiveButtonText);
        mPositiveButtonListener = listener;
    }

    public int getChoosenPosition() {
        return ((LanSearchResultAdapter) adapter).getChoosenPosition();
    }


    public void setNegativeButton(int textId, View.OnClickListener listener) {
        bt_negative.setVisibility(View.VISIBLE);
        mNegativeButtonText = mContext.getText(textId);
        bt_negative.setText(mNegativeButtonText);
        mNegativeButtonListener = listener;
    }

    public void setNegativeButton(CharSequence text, View.OnClickListener listener) {
        bt_negative.setVisibility(View.VISIBLE);
        mNegativeButtonText = text;
        bt_negative.setText(mNegativeButtonText);
        mNegativeButtonListener = listener;
    }

    private View.OnClickListener mBtnOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_positive:
                    mPositiveButtonListener.onClick(v);
                    break;
                case R.id.bt_negative:
                    mNegativeButtonListener.onClick(v);
                    break;
            }
        }
    };

    public void setContent(CharSequence content) {
        rl_string_content.setVisibility(View.VISIBLE);
        tv_content.setText(content);
    }

    public void setContent(CharSequence content, boolean leftAlign) {
        rl_string_content.setVisibility(View.VISIBLE);
        tv_content.setText(content);
        if (leftAlign) {
            tvCommand.setGravity(Gravity.LEFT);
        }
    }

    public void setContent(CharSequence content, int gravity) {
        rl_string_content.setVisibility(View.VISIBLE);
        tv_content.setText(content);
        tvCommand.setGravity(gravity);
    }

    public void setContent(int contentId) {
        rl_string_content.setVisibility(View.VISIBLE);
        tv_content.setText(mContext.getText(contentId));
    }

    public void setConditionContent(CharSequence titile, CharSequence content) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llCondition.setVisibility(View.VISIBLE);
        tvConditionTitle.setText(titile);
        tvCondition.setText(content);
    }

    public void setConditionContent(int titleId, int contentId) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llCondition.setVisibility(View.VISIBLE);
        tvConditionTitle.setText(mContext.getText(titleId));
        tvCondition.setText(mContext.getText(contentId));
    }

    public void setCommandContent(CharSequence titile, CharSequence content) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llCommnad.setVisibility(View.VISIBLE);
        tvCommandTitle.setText(titile);
        tvCommand.setText(content);
    }

    public void setCommandContent(int titleId, int contentId) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llCommnad.setVisibility(View.VISIBLE);
        tvCommandTitle.setText(mContext.getText(titleId));
        tvCommand.setText(mContext.getText(contentId));
    }

    public void setValidContent(CharSequence titile, CharSequence content) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llValid.setVisibility(View.VISIBLE);
        tvValidTitle.setText(titile);
        tvValid.setText(content);
    }

    public void setValidContent(int titleId, int contentId) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llValid.setVisibility(View.VISIBLE);
        tvValidTitle.setText(mContext.getText(titleId));
        tvValid.setText(mContext.getText(contentId));
    }

    public void setStatusContent(CharSequence titile, CharSequence content) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llStatus.setVisibility(View.VISIBLE);
        tvStatusTitle.setText(titile);
        tvStatus.setText(content);
    }

    public void setStatusContent(int titleId, int contentId) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llStatus.setVisibility(View.VISIBLE);
        tvStatusTitle.setText(mContext.getText(titleId));
        tvStatus.setText(mContext.getText(contentId));
    }

    public void setPushContent(CharSequence titile, CharSequence content) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llPush.setVisibility(View.VISIBLE);
        tvPushTitle.setText(titile);
        tvPush.setText(content);
    }

    public void setPushContent(int titleId, int contentId) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llPush.setVisibility(View.VISIBLE);
        tvPushTitle.setText(mContext.getText(titleId));
        tvPush.setText(mContext.getText(contentId));
    }

    public void setImportanceContent(CharSequence titile, CharSequence content) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llImportance.setVisibility(View.VISIBLE);
        tvImportanceTilte.setText(titile);
        tvImportance.setText(content);
    }

    public void setImportanceContent(int titleId, int contentId) {
        rl_auto_content.setVisibility(View.VISIBLE);
        llImportance.setVisibility(View.VISIBLE);
        tvImportanceTilte.setText(mContext.getText(titleId));
        tvImportance.setText(mContext.getText(contentId));
    }

    public void setTriggerCondition(DeviceBean deviceBean) {
        rl_rule_trigger_condition.setVisibility(View.VISIBLE);
        if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_PH) {
            ll_value_choose.setVisibility(View.GONE);
            ll_value_input.setVisibility(View.VISIBLE);
            tvSensorUnity.setText("");
        } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_TEMPERATURE) {
            ll_value_choose.setVisibility(View.GONE);
            ll_value_input.setVisibility(View.VISIBLE);
            tvSensorUnity.setText(mContext.getString(R.string.temperature_unity));
        } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
            ll_value_choose.setVisibility(View.VISIBLE);
            ll_value_input.setVisibility(View.GONE);
        }
    }

    public void setActionValue(int Type) {
        rl_rule_action_condition.setVisibility(View.VISIBLE);
        switch (Type) {
            case 1:
                rg_choose.setVisibility(View.VISIBLE);
                ll_sleep.setVisibility(View.GONE);
                break;
            case 2:
                rg_choose.setVisibility(View.GONE);
                ll_sleep.setVisibility(View.VISIBLE);
                break;
        }
    }

    public String checkSleep() {
        String sleepTime = et_sleep.getText().toString().trim();
        if (TextUtils.isEmpty(sleepTime)) {
            return null;
        }
        double value;
        try {
            value = Double.valueOf(sleepTime);
        } catch (Exception e) {
            return mContext.getString(R.string.input_need_number);
        }
        if (value < 0 || sleepTime.split("\\.").length > 1 || sleepTime.contains("\\.")) {
            return mContext.getString(R.string.input_need_int);
        }

        return "True";
    }

    public CommandBean getActionValue(DeviceBean deviceBean, int Type) {
        String sleepTime = et_sleep.getText().toString().trim();
        CommandBean commandBean = new CommandBean();
        int order = 0;
        if (RuleActivity.ruleBean.getCommands() == null) {
            order = 1;
        } else {
            order = RuleActivity.ruleBean.getCommands().size() + 1;
        }
        switch (Type) {
            case 1:
                commandBean.setType("commands");
                ArrayList<ActionBean> actionBeans = new ArrayList<ActionBean>();
                if (rb_open.isChecked()) {
                    ActionBean actionBean = new ActionBean();
                    actionBean.setValue(255);
                    actionBean.setField("sensorvalue");
                    actionBean.setOrder(1);
                    actionBean.setAddress(deviceBean.getAddress());
                    actionBeans.add(actionBean);
                } else {
                    ActionBean actionBean = new ActionBean();
                    actionBean.setValue(0);
                    actionBean.setField("sensorvalue");
                    actionBean.setOrder(1);
                    actionBean.setAddress(deviceBean.getAddress());
                    actionBeans.add(actionBean);
                }
                commandBean.setCommand(actionBeans);
                commandBean.setOrder(order);

//                if (!TextUtils.isEmpty(sleepTime)) {
//                    ActionBean actionBean = new ActionBean();
//                    actionBean.setValue(Double.valueOf(sleepTime));
//                    actionBean.setField("sleep");
//                    actionBean.setOrder(2);
//                    actionBeans.add(actionBean);
//                }
                break;
            case 2:
                commandBean.setType("sleep");
                commandBean.setValue(Integer.valueOf(sleepTime));
                commandBean.setOrder(order);
                break;
            case 3:
                commandBean.setType("notify");
                commandBean.setValue(0);
                commandBean.setOrder(order);

                break;
        }
        return commandBean;
    }


    public ActionBean getActionValueByScene(DeviceBean deviceBean, int Type) {
        String sleepTime = et_sleep.getText().toString().trim();
        int order = 0;
        if (SceneActivity.sceneBean.getCommands() == null) {
            order = 1;
        } else {
            order = SceneActivity.sceneBean.getCommands().size() + 1;
        }
        ActionBean actionBean = new ActionBean();
        switch (Type) {
            case 1:
                if (rb_open.isChecked()) {
                    actionBean.setValue(255);
                    actionBean.setField("sensorvalue");
                    actionBean.setOrder(order);
                    actionBean.setAddress(deviceBean.getAddress());
                } else {
                    actionBean.setValue(0);
                    actionBean.setField("sensorvalue");
                    actionBean.setOrder(order);
                    actionBean.setAddress(deviceBean.getAddress());
                }
                break;
            case 2:
                actionBean.setField("sleep");
                actionBean.setValue(Integer.valueOf(sleepTime));
                actionBean.setOrder(order);
                break;
        }
        return actionBean;
    }


    public TriggerBean getTriggerBean(DeviceBean deviceBean) {
        TriggerBean trigger = new TriggerBean();
        trigger.setAddress(deviceBean.getAddress());
        ArrayList<ValueConditionBean> valueConditionBeans = trigger.getValueConditionBeans();
        if (valueConditionBeans == null) {
            valueConditionBeans = new ArrayList<ValueConditionBean>();
            trigger.setValueConditionBeans(valueConditionBeans);
        } else {
            valueConditionBeans.clear();
        }
        if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
            if (rb_above.isChecked()) {
                double aboveValue = 2;
                ValueConditionBean valueConditionBean = new ValueConditionBean();
                valueConditionBean.setDuaration(0);
                valueConditionBean.setOp("=");
                valueConditionBean.setField("sensorvalue");
                valueConditionBean.setThreshold(aboveValue);
                valueConditionBeans.add(valueConditionBean);
            }
            if (rb_below.isChecked()) {
                double belowValue = 1;
                ValueConditionBean valueConditionBean = new ValueConditionBean();
                valueConditionBean.setDuaration(0);
                valueConditionBean.setOp("=");
                valueConditionBean.setField("sensorvalue");
                valueConditionBean.setThreshold(belowValue);
                valueConditionBeans.add(valueConditionBean);
            }
        } else {
            if (cbAbove.isChecked()) {
                String aboveValue = etTriggerValue.getText().toString().trim();
                if (!TextUtils.isEmpty(aboveValue)) {
                    ValueConditionBean valueConditionBean = new ValueConditionBean();
                    valueConditionBean.setDuaration(0);
                    valueConditionBean.setOp(">");
                    valueConditionBean.setField("sensorvalue");
                    valueConditionBean.setThreshold(DeviceConstant.convertValue(deviceBean.getSensorunit(), aboveValue));
                    valueConditionBeans.add(valueConditionBean);
                }
            } else {
                String belowValue = etTriggerValue.getText().toString().trim();
                if (!TextUtils.isEmpty(belowValue)) {
                    ValueConditionBean valueConditionBean = new ValueConditionBean();
                    valueConditionBean.setDuaration(0);
                    valueConditionBean.setOp("<");
                    valueConditionBean.setField("basicvalue");
                    valueConditionBean.setThreshold(DeviceConstant.convertValue(deviceBean.getSensorunit(), belowValue));
                    valueConditionBeans.add(valueConditionBean);
                }
            }
        }
        return trigger;
    }

    public String checkInput(DeviceBean deviceBean) {
        if (deviceBean.getFunctype() != DeviceConstant.FUNCTYPE_WATERLEVER) {
            String belowValue = etTriggerValue.getText().toString().trim();
            if (TextUtils.isEmpty(belowValue)) {
                return null;
            }
            double value;
            try {
                value = Double.valueOf(belowValue);
            } catch (Exception e) {
                return mContext.getString(R.string.input_need_number);
            }
            if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_TEMPERATURE) {
                if (value < 0 || value > 100) {
                    return mContext.getString(R.string.input_need_hundred);
                } else {
                    if (belowValue.split("\\.").length == 2) {
                        if (belowValue.split("\\.")[1].length() > 2) {
                            return mContext.getString(R.string.input_one_dot);
                        }
                    } else if (belowValue.split("\\.").length == 1 && belowValue.contains("\\.")) {
                        return mContext.getString(R.string.input_need_hundred);
                    } else if (belowValue.split("\\.").length > 2) {
                        return mContext.getString(R.string.input_need_hundred);
                    }
                }
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_PH) {
                if (value < 0 || value > 14) {
                    return mContext.getString(R.string.input_need_forteen);
                } else {
                    if (belowValue.split("\\.").length == 2) {
                        if (belowValue.split("\\.")[1].length() > 2) {
                            return mContext.getString(R.string.input_two_dot);
                        }
                    } else if (belowValue.split("\\.").length == 1 && belowValue.contains("\\.")) {
                        return mContext.getString(R.string.input_need_forteen);
                    } else if (belowValue.split("\\.").length > 2) {
                        return mContext.getString(R.string.input_need_forteen);
                    }
                }
            }
            return "True";
        }
        return "True";
    }


    public void setBackGround(int drawableid) {
        rl_background.setBackgroundResource(drawableid);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setBackGround(Drawable drawable) {
        rl_background.setBackground(drawable);
    }

}
